package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.ApnModeAddedAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;

/**
 * APN新增方式联动设值APN类型
 * Created by huanghaosheng on 2020/11/25 14:53
 */
@Service
@AttrHandler(ApnModeAddedAttrHandler.class)
public class ApnModeAddedAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    OperateInstAttrService operateInstAttrService;
    
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    private static final String APNNAMEDZVALUE = "100006952";

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        if (!KiteObjectUtils.isEmpty(attr) && KiteStringUtils.isEqual(attr.getValue(), APNNAMEDZVALUE)) {
            // APN类型设值"互联网"
            KiteAttr apnLxkiteAttr = inst.getAttr("200003999");
            if (!KiteObjectUtils.isEmpty(apnLxkiteAttr)) {
                operateInstAttrService.setAttrValue(inst, "200003999", "", data.getSceneInstId(), null);
                operateInstAttrService.setAttrValue(inst, "200003999", "100003262", data.getSceneInstId(), null);
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {
        if (KiteStringUtils.equals("100006952", attr.getValue())) {
            List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(data.getSceneInstId());
            kiteProdInstList.stream().forEach(kiteProdInst -> {
                String prodId = kiteProdInst.getProdId();
                // 定制连接类3/4G、NB，APN类型去掉MEC
                if (KiteStringUtils.equalsAny(prodId, ProdCateUtil.getMainMemberProdIdForArr(ProdCateUtil.GIFT_DZ4G_PROD,
                    ProdCateUtil.GIFT_DZCAT1_PROD, ProdCateUtil.DZNB_PROD, ProdCateUtil.DZ4G_PROD, ProdCateUtil.GIFT_ZNWB_PROD,
                    ProdCateUtil.GIFT_JRSK_PROD, ProdCateUtil.GIFT_JRBW_PROD))) {
                    Optional.ofNullable(inst.getAttr("200003999"))
                        .ifPresent(kiteAttr -> {
                            List<KiteAttrValue> attrValues = kiteAttr.getAttrValues();
                            attrValues = attrValues.stream().filter(attrValue -> !KiteStringUtils.equals("100007260",attrValue.getAttrValue())).collect(Collectors.toList());
                            kiteAttr.setAttrValues(attrValues);
                        });
                }

            });
        }
        KiteAttr pcrfAppendAttr = inst.getAttr("200004167");
        KiteAttr pcrfAttr = inst.getAttr("200004005");
        if (!KiteStringUtils.equals(attr.getValue(), "100006952")) {
            Optional.ofNullable(inst.getAttr("200004004")).ifPresent(
                funcAttr -> {
                    funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                });
            Optional.ofNullable(inst.getAttr("200004005")).ifPresent(
                funcAttr -> {
                    funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                });
            Optional.ofNullable(inst.getAttr("200004167")).ifPresent(
                funcAttr -> {
                    funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                });
            
            Optional.ofNullable(inst.getAttr("200002833")).ifPresent(kiteAttr -> {
                kiteAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
            });
            Optional.ofNullable(inst.getAttr("200004135")).ifPresent(kiteAttr -> {
                kiteAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
            });
        } else {
            // 是否pcrf默认为是
            if (!KiteObjectUtils.isEmpty(pcrfAppendAttr) && !KiteObjectUtils.isEmpty(pcrfAttr) && "1_0000106".equals(pcrfAttr.getValue())) {
                pcrfAppendAttr.setShowType("D");
            }
        }
    }
}
